package com.buba.personnel.dao.impl;

import com.buba.personnel.dao.departmentDao;
import com.buba.personnel.pojo.department;
import com.buba.util.DBPool;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class departmentDaoImpl implements departmentDao {
    //声明dbutiles的数据库操作类，将数据源传入
    QueryRunner queryRunner = new QueryRunner(DBPool.getDS());

    /**
     * 查询部门
     * @param param
     * @return
     */
    @Override
    public List<department> selDep(Map<String, String> param) {
        String sql = "select * from departinfo where 1=1";
        //当前页和每页的条数
        int page = Integer.parseInt(param.get("page")) ;
        int limit = Integer.parseInt(param.get("limit"));
        //计算limit的第一个参数
        int index = (page - 1) * limit;
        //将分页拼接到sql语句中
        sql += " limit " + index + "," + limit;
        try {
            return queryRunner.query(sql,new BeanListHandler<>(department.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    /**
     * 添加
     * @param param
     */
    @Override
    public void insert(Map<String, String> param) {
        //编写sql语句
        String sql = "insert into departinfo values(null,?,?,?,?,?)";
        //执行sql
        Object[] objects = new Object[5];
        objects[0] = param.get("departName");
        objects[1] = param.get("sellist");
        objects[2] = param.get("CTel");
        objects[3] = param.get("CMTel");
        objects[4] = param.get("faxes");
        try {
            queryRunner.update(sql,objects);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public int updDepartment(Map<String, String> param) {
        //编写sql
        String sql = "update departinfo set DepartName=?,PrincipalUser=?,ConnectTelNo=?,ConnectMobileTelNo=?,Faxes=? where DepartId=?";
        //给占位符赋值
        Object[] objects = new Object[6];
        objects[0] = param.get("departName");
        objects[1] = param.get("principalUser");
        objects[2] = param.get("connectTelNo");
        objects[3] = param.get("connectMobileTelNo");
        objects[4] = param.get("faxes");
        objects[5] = param.get("departId");
        try {
            return queryRunner.update(sql,objects);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除方法
     * @param departId
     * @return
     */
    @Override
    public int delDep(String departId) {
        String sql = "delete from departinfo where DepartId="+departId;
        try {
            return queryRunner.update(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    /**
     * 查询总条数
     * @param param
     * @return
     */
    @Override
    public long selCount(Map<String, String> param) {
        String sql = "select count(*) from departinfo where 1=1";
        try {
            return queryRunner.query(sql,new ScalarHandler<>());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
}
